<?php

namespace BackendBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class ApplyController extends Controller
{
    public function applyListAction(Request $request)
    {
        if (!$this->get('session')->get('admin_name')) {
            return $this->render('@Backend/auth/login.html.twig');
        }
        $status = $request->get('status');
        $year = $request->get('year');
        $name = $request->get('n');
        $status_sql = $status == '' ? "(a.status = 1 OR a.status = 0 OR a.status = 2)" : "a.status = '". $status ."'";

        $em    = $this->get('doctrine.orm.entity_manager');
        if ($year === null) {
            $dql   = "SELECT a FROM BaseBundle:Apply a ORDER BY a.id DESC";
        } else {
            $dql   = "SELECT a FROM BaseBundle:Apply a WHERE a.year = '". $year ."' AND $status_sql AND (a.name LIKE '%". $name ."%' OR a.age LIKE '%". $name ."%' OR a.mobile LIKE '%". $name ."%') ORDER BY a.id DESC";
        }

        $query = $em->createQuery($dql);

        $paginator  = $this->get('knp_paginator');
        $pagination = $paginator->paginate(
            $query, /* query NOT result */
            $request->query->getInt('page', 1)/*page number*/,
            15 /*limit per page*/
        );

        // 最近5年的年份
        $years = array();
        $currentYear = date('Y');
        for ($i=-1; $i<4; $i++)
        {
            $years[$i] = $currentYear + $i;
        }

        return $this->render('BackendBundle:homePage:homePage.html.twig', array('pagination' => $pagination, 'yearData' => $years));
    }

}
